package com.dcivision.framework;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SqlAccountUtility {
	protected static Log log = LogFactory.getLog(SqlAccountUtility.class.getClass().getName());
  public static boolean deleteRecordFromSql(Connection conn, String tableName, String contion) {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized (conn) {
      try {
        sqlStat.append("DELETE ");
        sqlStat.append(" FROM   " + tableName);
        sqlStat.append(" WHERE  " + contion);
        preStat = conn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
        //setPrepareStatement(preStat, 1, workflowStepID);
        preStat.executeUpdate();
        return true;
      } catch (SQLException sqle) {
        log.error(sqle);
      } catch (Exception e) {
    	  log.error(e);
      } finally {
        try {
          preStat.close();
        } catch (Exception ignore) {
        } finally {
          preStat = null;
        }
      }
    }
    return false;
  }

  public static int getIDCountFromSql(Connection conn, String tableName, String contion) {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    ResultSet rs = null;
    int result = 0;
    synchronized (conn) {
      try {
        sqlStat.append("SELECT COUNT(ID)  ");
        sqlStat.append(" FROM   " + tableName);
        sqlStat.append(" WHERE  " + contion);
        preStat = conn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
        rs = preStat.executeQuery();
        rs.next();
        result = rs.getInt(1);
      } catch (SQLException sqle) {
    	  log.error(sqle);
      } catch (Exception e) {
    	  log.error(e);
      } finally {
        try {
          preStat.close();
        } catch (Exception ignore) {
        } finally {
          preStat = null;
        }
      }
    }
    return result;
  }

  public static Integer getIntegerFieldValueFromFieldName(Connection conn, String tableName, String fieldName,
      String contion) {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    ResultSet rs = null;
    Integer result = null;
    synchronized (conn) {
      try {
        sqlStat.append("SELECT " + fieldName);
        sqlStat.append(" FROM   " + tableName);
        sqlStat.append(" WHERE  " + contion);
        preStat = conn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
        rs = preStat.executeQuery();
        if (rs.next()) {
          result = new Integer(rs.getInt(fieldName));
        }
      } catch (SQLException sqle) {
    	  log.error(sqle);
      } catch (Exception e) {
    	  log.error(e);
      } finally {
        try {
          preStat.close();
        } catch (Exception ignore) {
        } finally {
          preStat = null;
        }
      }
    }
    return result;
  }
  //king---add---start
  public static List getIntegerListFieldValueFromFieldName(Connection conn, String tableName, String fieldName,
	      String contion) {
	    PreparedStatement preStat = null;
	    StringBuffer sqlStat = new StringBuffer();
	    ResultSet rs = null;
	    List result = new ArrayList();
	    synchronized (conn) {
	      try {
	        sqlStat.append("SELECT " + fieldName);
	        sqlStat.append(" FROM   " + tableName);
	        sqlStat.append(" WHERE  " + contion);
	        preStat = conn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE,
	            ResultSet.CONCUR_READ_ONLY);
	        rs = preStat.executeQuery();
	        while (rs.next()) {
	          Integer id=new Integer(rs.getInt(fieldName));
	          result.add(id);
	        }
	      } catch (SQLException sqle) {
	    	  log.error(sqle);
	      } catch (Exception e) {
	    	  log.error(e);
	      } finally {
	        try {
	          preStat.close();
	        } catch (Exception ignore) {
	        } finally {
	          preStat = null;
	        }
	      }
	    }
	    return result;
	  }
  //king---add---end
}
